package com.apes.fn.scm.saleReport.service.process.orderProcess;

import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.fn.crm.customerExpand.FnCustomerExpandRepository;
import com.apes.fn.scm.saleReport.service.process.CreateDetailsProcess;
import com.apes.fn.scm.vipSettle.model.VipUnionSettle;
import com.apes.fn.scm.vipSettle.model.VipUnionSettleItem;
import com.apes.fn.scm.vipSettle.repository.VipUnionSettleRepository;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.car.model.CarConfiguration;
import com.apes.scm.masterdata.car.model.CarLabel;
import com.apes.scm.masterdata.car.model.CarModel;
import com.apes.scm.masterdata.car.repository.CarConfigurationRepository;
import com.apes.scm.masterdata.product.model.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;

/**
 * 会员联盟生成营业额明细
 */
@Service
public class VipUnionProcess extends CreateDetailsProcess {
    @Autowired
    private VipUnionSettleRepository vipUnionSettleRepository;
    @Autowired
    private CarConfigurationRepository carConfigurationRepository;
    @Autowired
    private FnCustomerExpandRepository fnCustomerExpandRepository;

    @Override
    public void process(SimpleRequest request) {
        String id = request.get("id");
        VipUnionSettle vipUnionSettle = vipUnionSettleRepository.findOne(id);
        //根据商品分组
        Map<Product, List<VipUnionSettleItem>> vipUnionSettleItemsMap = vipUnionSettle.getVipUnionSettleItems().stream().collect(Collectors.groupingBy(VipUnionSettleItem::getProduct));
        vipUnionSettleItemsMap.forEach((k,v) -> createDetails(MapUtil.mapper("order", v)));
    }

    /**
     * @param map {"order": InternalHelpReturnApprovalItem}
     *
     */
    @Override
    public Map<String, Object> getModel(Map map) {
        List<VipUnionSettleItem> vipUnionSettleItemList = (List<VipUnionSettleItem>) map.get("order");
        VipUnionSettle vipUnionSettle = vipUnionSettleItemList.get(0).getVipUnionSettle();
        Product product = vipUnionSettleItemList.get(0).getProduct();
        String fnCustomerExpandId = vipUnionSettle.getFnCustomerExpand().getId();
        //厂牌
        CarLabel carLabel = null;
        //车型
        CarModel carModel = null;
        if(fnCustomerExpandId != null){
            //车辆扩展信息
            FnCustomerExpand fnCustomerExpand = fnCustomerExpandRepository.findOne(fnCustomerExpandId);
            String jekunModelCode = fnCustomerExpand.getJekunModelCode();
            if(jekunModelCode != null) {
                //配置车型
                CarConfiguration carConfiguration = carConfigurationRepository.findOne(jekunModelCode);
                if(carConfiguration != null){
                    carLabel = carConfiguration.getCarLabel();
                    carModel = carConfiguration.getCarModel();
                }
            }
        }
        AtomicReference<Double> vipUnionOutAmount = new AtomicReference<>(0.00);
        vipUnionSettleItemList.forEach(vipUnionSettleItem -> {
            if(vipUnionSettleItem.getVipBalanceType() == 2){
                vipUnionOutAmount.updateAndGet(v -> (v + vipUnionSettleItem.getCouponFlow()));
            } else {
                vipUnionOutAmount.updateAndGet(v -> (v + vipUnionSettleItem.getSettlementAmount()));
            }
        });

        String originItem = vipUnionSettleItemList.stream().map(VipUnionSettleItem::getId).collect(Collectors.joining(","));
        return MapUtil.mapper("carLicense",vipUnionSettle.getCarLicense(),"product",product,
                "productUom",vipUnionSettleItemList.get(0).getUom(),"qty",(double)vipUnionSettleItemList.size(),"salesAmount",0.00,"vipUnionOutAmount",vipUnionOutAmount.get(),
                "serveType",product.getServeType(),"dept",vipUnionSettle.getDept(),"company",vipUnionSettle.getCompany(),
                "origin",vipUnionSettle.getId(),"originItem",originItem,"originBusinessType","vipUnionSettle",
                "originCreateDate",vipUnionSettle.getStatisticDate(),"source", vipUnionSettle.getOrigin(),"sourceBusinessType",vipUnionSettle.getOriginBusinessType(),
                "productGroup",product.getProductGroup(),"classify",product.getClassify(),
                "brand",product.getBrand(),"tax", vipUnionSettleItemList.get(0).getTax(),"carLabel",carLabel,"carModel",carModel,
                "pftPriceUnit",0.00,"priceUnit",0.00, "discountAmount",0.00,"couponDiscountAmount",0.00);
    }

    @Override
    public boolean support(String type) {
        return "vipUnionSettle".equals(type);
    }

}
